﻿
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Master.Dao;
using Shpm.App.Master.Bean;
using Shpm.System;
using log4net;
using Shpm.App.Commons.Utils;

public partial class App_Input_WorkTemplateSrhForm : System.Web.UI.Page
{
    // log
    protected static ILog log = LogManager.GetLogger(typeof(App_Input_WorkTemplateSrhForm));
  
    // page load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            // 接收传递参数并初始化控件
            txtName.Text = Convert.ToString(Context.Items["InpName"]);

            // 接收传递参数为检索条件
            ViewState["HidName"] = Convert.ToString(Context.Items["HidName"]);

            // 接收传递参数为分页编号 
            if (Context.Items.Contains("HidPageIndex"))
            {
                viewSrhRst.PageIndex = Convert.ToInt32(Context.Items["HidPageIndex"]);
            }

            // is from update page (delete operation)
            if (Context.Items.Contains("DelFlg") 
                && "1".Equals(Convert.ToString(Context.Items["DelFlg"])))
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0003").ToString();            
            }
        }
    }

    // before render
    void Page_PreRender(object sender, EventArgs e)
   {        
        DailyWorkTemplateDao dao = new DailyWorkTemplateDao();
        DailyWorkTemplateBean bean = new DailyWorkTemplateBean();
        IList lstRst = null;

        tableRst.Visible = true;
        bean.TemplateName = Convert.ToString(ViewState["HidName"]);        
        lstRst = dao.ListWorkTemplate(bean);        
        if (lstRst == null || lstRst.Count == 0)
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0005").ToString();
            tableRst.Visible = false;
        }
        viewSrhRst.DataSource = lstRst;
        viewSrhRst.DataBind();
    }

    // page index change
    protected void viewSrhRst_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 设置分页编号
        viewSrhRst.PageIndex = e.NewPageIndex;
    }
   
    // details click
    protected void viewSrhRst_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (Request["x"] == null || Request["y"] == null)
        { return; }

        if (e.CommandName == "Details")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            // Get the Key value from the GridView control.
            DataKey key = viewSrhRst.DataKeys[index];
            string templateId = key.Value.ToString();
            
            // 传递编号及状态
            Context.Items["TEMPLATE_ID"] = templateId;
            gotoPage("~/App/Master/WorkTemplateUpdForm.aspx");
        }
    }

    // 新建
    protected void btnNew_Click(object sender, EventArgs e)
    {
        Context.Items["TEMPLATE_ID"] = "";
        gotoPage("~/App/Master/WorkTemplateUpdForm.aspx");
    }

    // 检索
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        ViewState["HidName"] = txtName.Text;        
        viewSrhRst.PageIndex = 0;
    }

    // 参数传递 and 页面迁移
    private void gotoPage(string url)
    {
        // 传递控件输入内容
        Context.Items["InpName"] = txtName.Text;
       
        // 传递检索条件
        Context.Items["HidName"] = Convert.ToString(ViewState["HidName"]);       

        // 传递分页编号
        Context.Items["HidPageIndex"] = viewSrhRst.PageIndex;
        ControllerUtils.Transfer(Context, url, true);
    }

   
}
